/**
 * git服务操作
 *
 * Created by snowingsea on 2020/02/06.
 */
/**
 * 获取git用户名
 * @param {User} user 用户
 * @param {Number} limit 长度限制
 * @return {String}
 */
function getGitUserName(user, limit = 35) {
  return user._id.toString().substr(0, limit); // eslint-disable-line no-underscore-dangle
  // const userId = user._id.toString(); // eslint-disable-line no-underscore-dangle
  // const regexp = /[^a-z|A-Z|0-9|\-|_|]/g;
  // const nameLimit = limit - userId.length - 1;
  // return `${user.username.replace(regexp, '-').substr(0, nameLimit)}_${userId}`;
}

/**
 * 获取git项目名
 * @param {Project} project 项目
 * @param {Number} limit 长度限制
 * @return {String}
 */
function getGitRepoName(project, limit = 100) {
  return project._id.toString().substr(0, limit); // eslint-disable-line no-underscore-dangle
  // const projectId = project._id.toString(); // eslint-disable-line no-underscore-dangle
  // const regexp = /[^a-z|A-Z|0-9|\-|_|]/g;
  // const nameLimit = limit - projectId.length - 1;
  // return `${project.projectName.replace(regexp, '-').substr(0, nameLimit)}_${projectId}`;
}

/**
 * 获取版本分支名
 * @param {ProjectVersion} projectVersion 项目版本信息
 * @param {Number} limit 长度限制
 * @return {String}
 */
function getBranchName(projectVersion, limit = 200) {
  if (projectVersion.masterVersionId) {
    return projectVersion.masterVersionId.toString().substr(0, limit);
  }
  return projectVersion._id.toString().substr(0, limit);
  // eslint-disable-line no-underscore-dangle
  // const versionId = projectVersion._id.toString(); // eslint-disable-line no-underscore-dangle
  // const regexp = /[^a-z|A-Z|0-9|\-|_|]/g;
  // const nameLimit = limit - versionId.length - 1;
  // return `${projectVersion.versionName.replace(regexp, '-').substr(0, nameLimit)}_${versionId}`;
}

/**
 * 获取远程url
 * @param {User} user 用户信息
 * @param {Project} project 项目信息
 * @return {String}
 */
function getRemoteURL(user, project) {
  const userName = getGitUserName(user);
  const projectName = getGitRepoName(project);
  const { protocol, host, port } = global.config.storage.git;
  return `${protocol}://${host}:${port}/${userName}/${projectName}`;
}

/**
 * Export model definition object.
 */
module.exports = {
  getGitUserName,
  getGitRepoName,
  getBranchName,
  getRemoteURL,
};
exports = Object.assign(exports, module.exports);
